#include <iostream>
#include <string>
#include <vector>

#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>

typedef OpenMesh::PolyMesh_ArrayKernelT<> My_PolyMesh;

using namespace std;

int main()
{
	My_PolyMesh myPolyMesh;
	My_PolyMesh::VertexHandle vhandle[8];
	string filename_out = "../data/cube.obj";

	vhandle[0] = myPolyMesh.add_vertex(My_PolyMesh::Point(-1, -1, 1));
	vhandle[1] = myPolyMesh.add_vertex(My_PolyMesh::Point(1, -1, 1));
	vhandle[2] = myPolyMesh.add_vertex(My_PolyMesh::Point(1, 1, 1));
	vhandle[3] = myPolyMesh.add_vertex(My_PolyMesh::Point(-1, 1, 1));
	vhandle[4] = myPolyMesh.add_vertex(My_PolyMesh::Point(-1, -1, -1));
	vhandle[5] = myPolyMesh.add_vertex(My_PolyMesh::Point(1, -1, -1));
	vhandle[6] = myPolyMesh.add_vertex(My_PolyMesh::Point(1, 1, -1));
	vhandle[7] = myPolyMesh.add_vertex(My_PolyMesh::Point(-1, 1, -1));

	// generate (quadrilateral) faces

	std::vector<My_PolyMesh::VertexHandle>  face_vhandles;

	face_vhandles.clear();
	face_vhandles.push_back(vhandle[0]);
	face_vhandles.push_back(vhandle[1]);
	face_vhandles.push_back(vhandle[2]);
	face_vhandles.push_back(vhandle[3]);
	myPolyMesh.add_face(face_vhandles);

	face_vhandles.clear();
	face_vhandles.push_back(vhandle[7]);
	face_vhandles.push_back(vhandle[6]);
	face_vhandles.push_back(vhandle[5]);
	face_vhandles.push_back(vhandle[4]);
	myPolyMesh.add_face(face_vhandles);

	face_vhandles.clear();
	face_vhandles.push_back(vhandle[1]);
	face_vhandles.push_back(vhandle[0]);
	face_vhandles.push_back(vhandle[4]);
	face_vhandles.push_back(vhandle[5]);
	myPolyMesh.add_face(face_vhandles);

	face_vhandles.clear();
	face_vhandles.push_back(vhandle[2]);
	face_vhandles.push_back(vhandle[1]);
	face_vhandles.push_back(vhandle[5]);
	face_vhandles.push_back(vhandle[6]);
	myPolyMesh.add_face(face_vhandles);

	face_vhandles.clear();
	face_vhandles.push_back(vhandle[3]);
	face_vhandles.push_back(vhandle[2]);
	face_vhandles.push_back(vhandle[6]);
	face_vhandles.push_back(vhandle[7]);
	myPolyMesh.add_face(face_vhandles);

	face_vhandles.clear();
	face_vhandles.push_back(vhandle[0]);
	face_vhandles.push_back(vhandle[3]);
	face_vhandles.push_back(vhandle[7]);
	face_vhandles.push_back(vhandle[4]);
	myPolyMesh.add_face(face_vhandles);

	// write mesh to output.obj
	try
	{
		if (!OpenMesh::IO::write_mesh(myPolyMesh, filename_out))
		{
			std::cerr << "Cannot write mesh to file" << filename_out << std::endl;
			return 1;
		}
	}
	catch (std::exception& x)
	{
		std::cerr << x.what() << std::endl;
		return 1;
	}

	system("pause");
	return 0;
}

